package com.yanxu.lock.upanddown;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025/3/23-2025
 * @Description: 不允许升级
 */
public class NotAllowUp {

    static ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
    static ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();
    static ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();

    public static void main(String[] args) {
        readLock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + " 获取到了读锁!");
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + " 尝试升级，获取写锁!!!");
            writeLock.lock();
            System.out.println(Thread.currentThread().getName() + " 升级成功，获取到了写锁!!!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            writeLock.unlock();
            readLock.unlock();
        }
    }
}